package nl.depository.pim.pages;

import nl.depository.pim.annotations.AnonymousAccess;
import nl.depository.pim.security.AuthenticationException;
import nl.depository.pim.services.Authenticator;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Secure;
import org.apache.tapestry5.beaneditor.Validate;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

@Secure
@AnonymousAccess
public class Login
{
	@Validate("required")
	@Property
	private String username = "admin";
	
	@Validate("required")
	@Property
	private String password = "admin123";
	
	@Component
	private Form loginForm;
	
	@Inject
	private Authenticator authenticator;
	
	@Inject
	private Messages messages;
	
	public Object onSubmitFromLoginForm()
	{
		Object page = null;
		
		try
		{
			authenticator.login(username, password);
			page = Index.class;
		}
		catch (AuthenticationException e)
		{
			loginForm.recordError(messages.get("error.login"));
		}
		
		return page;
	}
}
